<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Hash;
class ClassController extends Controller
{
    //后台用户添加页
    public function getAdd()
    {
        //跳转添加页
        return view('admin.class.add');
        // return view('admin.index');

    }
    //用户信息添加到数据库
    public function postInsert(Request $request)
    {


            $this->validate($request, [
                    'classname' => 'required',
                    
                ],[
                    'classname.required' => '分类名不能为空',
                    
                ]);
        // 接受分类信息
        $date = $request->input('classname');

   
        $res = DB::table('class')->insert(['classname' => $date]);
        // 判断处理结果 
        if ($res) {

               return  redirect('/admin/class/list');
        }else{
            return black();
        }
    
    }
    
      public function getList(Request $request)
        {
            //查询值 显示十条
                $res = DB::table('class')->paginate(10);
            //分配变量
            return view('admin.class.list', ['res' => $res]);
       
        }
    
    //删除数据 接受ajax值 返回相应的值给ajax
        public function getDelete(Request $request)
        {   
            $id = $request->input('id');
            $res = DB::table('class')->where('id',$id)->delete();
            if($res != 0){
                echo 1;
            }
        }
    //修改用户信息
        public function getEdit(Request $request)
        {   
            // 获取id
            $id = $request->input('id');
            // 查询id
            $res = DB::table('class')->where('id',$id)->first();
            //跳转到 修改显示页面
            return view('admin.class.edit', ['res' => $res]);
          
        }

        public function postUpdate(Request $request)
        {   
            // //检查修改内容
            // // dd($request->all());
            $this->validate($request, [
                'classname' => 'required|regex:/\w{6,18}/',
            
            ],[
                'class.required' => '用户名不能为空',
                'classname.regex' => '用户名6-18位',
             
            ]);
            $id = $request->input('id');
            $date['classname'] = $request->input('classname');
            // dd($date);
            $qq =  DB::table('class')
                    ->where('id', $id)
                    ->update($date);
            //判断修改
                    // dd($qq);
                    if($qq == 1){
                        return redirect('/admin/class/list');  
                    }else{
                         return back();
                    }

        }
      


}
